﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class BtnsDAL
    {
        public static List<BtnsInfo> List = FindAll();
        public static int Save(BtnsInfo info)
        {
            string sql = string.Format("insert into [btns]([Name],[Method],[Sort],[Color]) values(@Name,@Method,@Sort,@Color);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Method", info.Method),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@Color", info.Color)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(BtnsInfo info)
        {
            string sql = string.Format("update [btns] set [Name]=@Name,[Method]=@Method,[Sort]=@Sort,[Color]=@Color where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Method", info.Method),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@Color", info.Color),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static BtnsInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[btns]");
            return Simple_Data(dr);
        }
        
        public static List<BtnsInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [btns]  order by Sort asc"));
            List<BtnsInfo> list = new List<BtnsInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        private static BtnsInfo Simple_Data(DataRow dr)
        {
            BtnsInfo info = new BtnsInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Method = DataConvert.ToString(dr["Method"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.Color = DataConvert.ToString(dr["Color"]);
            }
            return info;
        }
        
    }
}
